package org.example.k8s_cloudplatform.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.models.V1Service;
import jakarta.annotation.Resource;
import org.example.k8s_cloudplatform.K8S_mapper.K8SPodMapper;
import org.example.k8s_cloudplatform.K8S_mapper.ServiceMapper;
import org.example.k8s_cloudplatform.entity.Pod;
import org.example.k8s_cloudplatform.entity.res.Result;
import org.example.k8s_cloudplatform.mapper.PodMapper;
import org.example.k8s_cloudplatform.mapper.PodRequestMapper;
import org.example.k8s_cloudplatform.entity.PodRequest;
import org.example.k8s_cloudplatform.service.Impl.PodRequestServiceImpl;
import org.example.k8s_cloudplatform.utils.K8S_util;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Calendar;

@Service
public class PodRequestService extends ServiceImpl<PodRequestMapper, PodRequest> implements PodRequestServiceImpl {
    @Resource
    private PodRequestMapper podRequestMapper;
    @Resource
    private PodMapper podMapper;

    @Resource
    private K8SPodMapper k8SPodMapper;

    @Resource
    private ServiceMapper serviceMapper;
    @Transactional
    public Result updateStatus(PodRequest podRequest) {
        podRequestMapper.updateById(podRequest);
        Integer nodePort;
        if(podRequest.getStatus() == 1 && podRequest.getType() == 0) {
            //创建pod
            Pod pod= K8S_util.toPod(podRequest);
            try {
                k8SPodMapper.create(pod);
            } catch (ApiException e) {
                e.printStackTrace();
                System.out.println(e.getResponseBody());
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                if(e.getCode() == 409)
                    return Result.exception("Pod名称重复,请检查集群状态");
                else
                    return Result.exception("Docker自动部署出错");
            }
            //创建对应的端口转发service
            try {
                V1Service temp = serviceMapper.createByPod(pod);
                //noinspection DataFlowIssue
                nodePort = temp.getSpec().getPorts().get(0).getNodePort();
            } catch (ApiException e) {
                e.printStackTrace();
                System.out.println(e.getResponseBody());
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return Result.exception("Service创建出错");
            }
            pod.setNodePort(nodePort);
            podMapper.insert(pod);
        }
        if(podRequest.getStatus() == 1 && podRequest.getType() == 1) {
            //续租
            Integer dockerID=podRequest.getDockerID();
            Pod pod=podMapper.selectById(dockerID);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(pod.getExpirationTime());
            calendar.add(Calendar.DAY_OF_MONTH, podRequest.getApplyTime());
            pod.setExpirationTime(calendar.getTime());

            podMapper.updateById(pod);
        }
        return Result.success();
    }

    //此方法保证为续租申请填写dockerID
    public Result relet(Integer dockerID,Integer applyTime) {
        Pod pod=podMapper.selectById(dockerID);
        PodRequest podRequest=K8S_util.toPodRequest(pod);
        podRequest.setStatus(0);
        podRequest.setType(1);
        podRequest.setApplyTime(applyTime);
        podRequest.setDockerID(dockerID);
        podRequestMapper.insert(podRequest);
        return Result.success();
    }
}
